package com.sscaipiao.android.activity.ThreeD;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.RedBallSingleArrayActivity;
import com.sscaipiao.android.activity.DaLeTou.DaLeTouHistoryActivity;
import com.sscaipiao.android.activity.DaLeTou.DaLeTouPayActivity;
import com.sscaipiao.android.common.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Airay.Rong on 14-1-2.
 */
public class SanDZuLiuActivity extends RedBallSingleArrayActivity {
	private final static String LOTT_CODE = "f3d";
	private final static int MAX_BALL_NUM = 11;
	private final static int MIN_CHOOSE_NUM = 2;
	private final static Class HISTORY_ACTIVITY = SanDZuLiuHistoryActivity.class;
	private final static Class PAY_ACTIVITY = SanDZuLiuPayActivity.class;



	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.red_ball_array_activity);//UI 一致
		setupUI(true, false, true, true, true);
		setTitleText("3D组六");
		setInfo("所选号码与开奖号码相同（顺序不限），即为中奖");
		addRedBalls(MAX_BALL_NUM);
		setRadomBallNum(MIN_CHOOSE_NUM);
	}

	@Override
	public void onResume() {
		super.onResume();
		showProgressDialog();
		setLottInformation(getIntent(),LOTT_CODE);
		if (getLottLostValues()) {
			setupLostValues();
		}
		reCalculatorSum();
		closeProgressDailog();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_confirm == v.getId()) {
			doConfirm();
		}
		if (R.id.btn_show_history == v.getId()) {
			showLottHistory(HISTORY_ACTIVITY);
		}
	}
	private void doConfirm() {
		int nRedNum = queryChoseRedBallNum();
		List<Integer> redChooseBalls = queryChoseRedBallArray();

		if (nRedNum < MIN_CHOOSE_NUM ) {
			String text = "请选择" + MIN_CHOOSE_NUM + "个或以上红球";
			Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
		} else {
			List<String> choosen = user.getChoosen();
			if (choosen == null) {
				choosen = new ArrayList<String>();
			}
			String redBalls = "";
			String singleOrMult = "";

			String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_1);
			String number = String.format(sAgeFormat1, notecount() + "");

			for (int num : redChooseBalls) {
				if (num < 10) {
					redBalls += ("0" + num + Constants.STRING_SPLITE);
				} else {
					redBalls += (num + Constants.STRING_SPLITE);
				}
			}
			if (notecount() == 1) {
				singleOrMult = getResources().getString(R.string.double_color_ball_add_single);
			} else {
				singleOrMult = getResources().getString(R.string.double_color_ball_add_multiple);
			}

			String str = redBalls.substring(0, redBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
					singleOrMult + Constants.STRING_SPLITE_COMPLEX + number;

			if (!choosen.contains(str)) {
				choosen.add(str);
				user.setChoosen(choosen);
				user.setHistory(history);

				Intent intent = new Intent(this,PAY_ACTIVITY);
				Bundle b = new Bundle();
				b.putSerializable(Constants.USER, user);
				b.putSerializable("date", date);
				b.putSerializable("leftTime", leftTime);
				intent.putExtras(b);
				startActivity(intent);

			} else {
				Toast.makeText(this, "该组合已在购买列表中请重新选择", Toast.LENGTH_SHORT).show();
				return;
			}


		}


	}

}